﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neo4JConnector
{
    public class QueryConditionObject : ObjectPropertyBase
    {

        bool _isEvaluated = false;

        [FormulaProperty]
        [DisplayName("属性名")]
        public object Name { get; set; }

        [DisplayName("比较")]
        [ComboProperty]
        public Operation Op { get; set; } = Operation.匹配;

        [DisplayName("值")]
        [FormulaProperty(AcceptsReturn = true)]
        public object Value { get; set; }


        public async Task<QueryConditionObject> Evaluate(IServerCommandExecuteContext dataContext)
        {

            if (_isEvaluated)
            {
                return this;
            }

            var name = (await dataContext.EvaluateFormulaAsync(Name))?.ToString() ?? "";

            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("查询条件的属性名不能为空。");
            }

            var val = await dataContext.EvaluateFormulaAsync(Value);

            return new QueryConditionObject()
            {
                Name = name,
                Op = this.Op,
                Value = val,
                _isEvaluated = true
            };
        }
    }

    public enum Operation
    {
        [Description("Match：整数、布尔值、时间日期和文本相同")]
        匹配,
        [Description("NotMatch：整数、布尔值、时间日期和文本不相同")]
        不匹配,
        [Description("In：整数、时间日期和文本在数组里面")]
        在里面,
        [Description("NotIn：整数、时间日期和文本不在数组里面")]
        不在里面,
        [Description("Range（Gt）：数值和时间日期大于")]
        数值大于,
        [Description("Range（Lte）：数值和时间日期不大于")]
        数值不大于,
        [Description("Range（Lt）：数值和时间日期小于")]
        数值小于,
        [Description("Range（Gte）：数值和时间日期不小于")]
        数值不小于,
    }
}
